package cn.gotom.core.web.action;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import javax.servlet.http.Cookie;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.interceptor.CookiesAware;

import cn.gotom.coding.EncodingUtils;
import cn.gotom.core.pojos.StatusEnum;
import cn.gotom.core.pojos.User;
import cn.gotom.core.service.UserService;
import cn.gotom.core.web.Constants;
import cn.gotom.core.web.Counter;
import cn.gotom.core.web.UserCookie;

import com.google.inject.Inject;

public class LoginAction extends SupportAction implements CookiesAware
{

	private static final long serialVersionUID = 1L;
	@Inject
	private UserService userService;
	@Inject
	private Counter counter;

	private String username;
	private String password;
	private boolean rememberMe;
	private String goingToURL;

	private Map<String, String> cookiesMap;

	public String login() throws Exception
	{
		try
		{
			User user = userService.get("username", username);
			if (user != null)
			{
				if (!user.getPassword().trim().equals(password.trim()))
				{
					log.error("用户名或密码错误！");
					setActionmessage("用户名或密码错误！");
					return INPUT;
				}
				else if (user.getStatus() != StatusEnum.正常)
				{
					log.error(user.getUsername() + " 状态：" + user.getStatus());
					setActionmessage("用户当前状态不可登录！");
					return INPUT;
				}
				if (rememberMe)
				{
					UserCookie userCookie = new UserCookie(user.getUsername(), user.getPassword());

					Cookie cookie = new Cookie(Constants.COOKIE_REMEMBERME_KEY, userCookie.toString());
					cookie.setMaxAge(60 * 60 * 24 * 14);
					response.addCookie(cookie);

					SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd-MMM-yyyy HH:mm:ss z");
					String expires = sdf.format(new Date());
					response.setHeader("Set-Cookie", Constants.COOKIE_REMEMBERME_KEY + "=\"" + cookie.getValue() + "; Max-Age=" + cookie.getMaxAge() + "; Path=" + cookie.getPath() + ";expires=" + expires);

					if (cookiesMap != null)
					{
						cookiesMap.put(Constants.COOKIE_REMEMBERME_KEY, userCookie.toString());
					}
				}
				EncodingUtils.encoding(user, "cp850", "GBK");
				this.getSession().put(Constants.USER_SESSION_KEY, user);
				String goingToURL = (String) this.getSession().get(Constants.GOING_TO_URL_KEY);
				if (StringUtils.isNotEmpty(goingToURL))
				{
					setGoingToURL(goingToURL);
					this.getSession().remove(Constants.GOING_TO_URL_KEY);
				}
				else
				{
					log.debug(request);
					setGoingToURL(Constants.WELCOME);
				}
				return SUCCESS;
			}
			else
			{
				setActionmessage("用户名或密码错误！");
				return INPUT;
			}
		}
		catch (Exception e)
		{
			log.error(e.getMessage(), e);
			setActionmessage("用户名或密码错误！");
			return INPUT;
		}
	}

	public int getCounter()
	{
		return counter.increment();
	}

	public String getUsername()
	{
		return username;
	}

	public void setUsername(String username)
	{
		this.username = username;
	}

	public String getPassword()
	{
		return password;
	}

	public void setPassword(String password)
	{
		this.password = password;
	}

	public boolean isRememberMe()
	{
		return rememberMe;
	}

	public void setRememberMe(boolean rememberMe)
	{
		this.rememberMe = rememberMe;
	}

	public String getGoingToURL()
	{
		return goingToURL;
	}

	public void setGoingToURL(String goingToURL)
	{
		this.goingToURL = goingToURL;
	}

	@Override
	public void setCookiesMap(Map<String, String> arg0)
	{
		cookiesMap = arg0;
	}

}
